library(ggplot2) #For graphing
library(magrittr) #Pipes
library(dplyr) # for shorter function names. but still prefer dplyr:: stems
library(knitr) # dynamic documents
library(rmarkdown) # dynamic
library(kableExtra) # enhanced tables, see http://haozhu233.github.io/kableExtra/awesome_table_in_html.html
# library(TabularManifest) # exploratory data analysis, see https://github.com/Melinae/TabularManifest
requireNamespace("knitr", quietly=TRUE)
requireNamespace("scales", quietly=TRUE) #For formating values in graphs
requireNamespace("RColorBrewer", quietly=TRUE)
requireNamespace("dplyr", quietly=TRUE)
requireNamespace("DT", quietly=TRUE) # for dynamic tables
# requireNamespace("plyr", quietly=TRUE)
# requireNamespace("reshape2", quietly=TRUE) #For converting wide to long
# requireNamespace("mgcv, quietly=TRUE) #For the Generalized Additive Model that smooths the longitudinal graphs.
config <- config::get()
source("./scripts/common-functions.R") # reporting functions and quick views
# source("./scripts/graphing/graph-presets.R") # font and color conventions
# source("./scripts/graphing/graph-support.R") # font and color conventions
ggplot2::theme_set(ggplot2::theme_bw())
compute_epi_timeline <- function(d, n_deaths_first_day = 1) { #}, d_country ){
# browser()
# d <- ds_cgrt %>%
# # filter(country_code %in% c("ITA","FRA") ) %>%
# filter(country_code %in% c("AFG") ) %>%
# select(country_code, date, n_cases, n_deaths)
#
d_out <- d %>%
# dplyr::filter(country_code %in% unique(d_country$id)) %>%
dplyr::group_by(country_code) %>%
dplyr::mutate(
# this solution might be vulnerable to cases where some intermediate dates are missed
n_deaths_cum = cumsum(tidyr::replace_na(n_deaths,0))
,n_cases_cum = cumsum(tidyr::replace_na(n_cases,0))
,n_deaths_cum_per_1m = n_deaths_cum/n_population_2018*1000000
,n_cases_cum_per_1m = n_cases_cum/ n_population_2018*1000000
,cutoff_death = n_deaths_cum >= 1
,cutoff_case = n_cases_cum >= 1
,days_since_1death = cumsum(tidyr::replace_na(cutoff_death,0))
,days_since_1case = cumsum(tidyr::replace_na(cutoff_case,0))
,date_of_1death = lubridate::as_date(ifelse(days_since_1death==1,date, NA))
,date_of_1case = lubridate::as_date(ifelse(days_since_1case==1,date, NA))
,date_of_1death = min(date_of_1death, na.rm =T)
,date_of_1case = min(date_of_1case, na.rm =T)
,days_since_1death = (date - date_of_1death) %>% as.integer()
,days_since_1case = (date - date_of_1case) %>% as.integer()
) %>%
dplyr::ungroup() %>%
# dplyr::filter(epi_timeline > 0) %>%
dplyr::mutate(
days_since_exodus = as.integer(date - lubridate::date("2020-01-13")) # first case outside of china
,days_since_pandemic = as.integer(date - lubridate::date("2020-03-11")) # WHO declares pandemic
) %>%
select(-cutoff_death, - cutoff_case, -date_of_1death, -date_of_1case)
return(d_out)
}
# for testing the function:
# d_out <- ds0 %>% filter(country_code == "ITA") %>%
# select(
# country_code, date,n_cases, n_deaths, ConfirmedDeaths, ConfirmedCases
# ) %>%
# compute_epi_timeline()
# reference table for geographic units
ds_geo <- readr::read_csv("./data-public/metadata/world-geography.csv")
# ds_geo %>% glimpse()
ds_covid <- readr::read_csv(config$path_input_covid)
# ds_covid %>% glimpse()
# OxCGRT
ds_cgrt <- readr::read_rds("./data-unshared/derived/OxCGRT.rds")
# to keep it manageble during exploration
ds_cgrt <- ds_cgrt %>% select(country_code, date, StringencyIndex )
# ds_cgrt %>% glimpse()
# n_distinct(ds_cgrt$country_code)
# ds_covid$country_code %>% unique() %>% length()
# ds_cgrt$country_code %>% unique() %>% length()
ds0 <- ds_covid %>%
compute_epi_timeline() %>%
dplyr::left_join(
ds_cgrt
,by = c("date", "country_code")
) %>%
dplyr::left_join(
ds_geo %>% select(-country_name, -country_number),
by = c("country_code" )
) %>%
filter(
!is.na(country_label)
)
# ds0 %>% glimpse()
# Why 75 days after exodus should be the starting point?
# 1. Most countries have peaked in their response
d1 <- ds0 #%>% filter(oecd)
g1 <- ds0 %>%
# filter(country_code %in% ds_country$id) %>%
# filter(country_code == "ITA") %>%
ggplot(aes(x = days_since_exodus, y = StringencyIndex, group = country_label))+
geom_line( alpha = .1)+
geom_point(data = d1 %>% filter(days_since_1case == 1), size = 2, fill = "#1b9e77", color = "black", alpha = .5, shape = 21)+
geom_point(data = d1 %>% filter(days_since_1death == 1), size = 2, fill = "#d95f02", color = "black", alpha = .5, shape = 21)+
scale_x_continuous(breaks = seq(0,100, 25))+
labs(
title = "Timeline of countries' respones to COVID-19 as measured by the Stringency Index"
,y = "Stringency Index", x = "Days since first case outside of China (Jan 13, 2020)"
)+
geom_vline(xintercept = 58, linetype = "dotted")+
geom_vline(xintercept = 75, linetype = "dashed")+
geom_vline(xintercept = 100, linetype = "dashed", color = "red")
margings_for_plotly <- list(
l = 50,
r = 50,
b = 100,
t = 100,
pad = 4
)
g1 <- plotly::ggplotly(g1)
g1 %>% plotly::layout(autosize = F, width = 900, height = 600, margin = margings_for_plotly)
# g1 %>% plotly::layout(autosize = T)
. . . . . .
# 2. This is when the mortality curves starts going up
# d2 <- ds0 #%>% filter(oecd)
d2 <- ds0 %>% filter(!country_label %in% c("San Marino"))
g2 <- d2 %>%
# filter(country_code %in% ds_country$id) %>%
# filter(country_code == "ITA") %>%
ggplot(aes(x = days_since_exodus, y = n_deaths_cum_per_1m, group = country_label))+
geom_line( alpha = .2)+
geom_point(data = d2 %>% filter(days_since_1case == 1), size = 2, fill = "#1b9e77", color = "black", alpha = .5, shape = 21)+
geom_point(data = d2 %>% filter(days_since_1death == 1), size = 2, fill = "#d95f02", color = "black", alpha = .5, shape = 21)+
scale_x_continuous(breaks = seq(0,100, 25))+
labs(
title = "Timeline of COVID-19 deaths per 1 million"
,y = "Total Deaths per 1 million", x = "Days since first case outside of China (Jan 13, 2020)"
)+
geom_vline(xintercept = 58, linetype = "dotted")+
geom_vline(xintercept = 75, linetype = "dashed")+
geom_vline(xintercept = 100, linetype = "dashed", color = "red")
g2 <- plotly::ggplotly(g2)
g2 %>% plotly::layout(autosize = F, width = 900, height = 600, margin = margings_for_plotly)
# g2 %>% plotly::layout(autosize = T)
. . . . . .
# 3. Repositioning to the first death:
# d3 <- ds0 #%>% filter(oecd)
d3 <- ds0 %>% filter(!country_label %in% c("San Marino"))
g3 <- d3 %>%
# filter(country_code %in% ds_country$id) %>%
# filter(country_code == "ITA") %>%
ggplot(aes(x = days_since_1death, y = n_deaths_cum_per_1m, group = country_label))+
geom_line( alpha = .2)+
geom_point(data = d3 %>% filter(days_since_1case == 1), size = 2, fill = "#1b9e77", color = "black", alpha = .5, shape = 21)+
geom_point(data = d3 %>% filter(days_since_1death == 1), size = 2, fill = "#d95f02", color = "black", alpha = .5, shape = 21)+
scale_x_continuous(breaks = seq(-100,100, 25))+
labs(
title = "Timeline of COVID-19 deaths per 1 million (centered)"
,y = "Total Deaths (per 1 million)", x = "Days since first confirmed death in the country"
)
g3 <- plotly::ggplotly(g3)
g3 %>% plotly::layout(autosize = F, width = 900, height = 600, margin = margings_for_plotly)
# g3 %>% plotly::layout(autosize = T)
. . . . .
For the sake of documentation and reproducibility, the current report was rendered in the following environment. Click the line below to expand.
Environment
- Session info -------------------------------------------------------------------------------------------------------
setting value
version R version 3.6.3 (2020-02-29)
os Windows 10 x64
system x86_64, mingw32
ui RTerm
language (EN)
collate English_United States.1252
ctype English_United States.1252
tz America/New_York
date 2020-05-21
- Packages -----------------------------------------------------------------------------------------------------------
package * version date lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.2)
backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.1)
callr 3.4.3 2020-03-28 [1] CRAN (R 3.6.3)
cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3)
codetools 0.2-16 2018-12-24 [2] CRAN (R 3.6.3)
colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.1)
config 0.3 2018-03-27 [1] CRAN (R 3.6.3)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.2)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.2)
devtools 2.3.0 2020-04-10 [1] CRAN (R 3.6.3)
digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.3)
dplyr * 0.8.5 2020-03-07 [1] CRAN (R 3.6.3)
DT 0.13 2020-03-23 [1] CRAN (R 3.6.3)
ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.2)
evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.2)
fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.2)
farver 2.0.3 2020-01-16 [1] CRAN (R 3.6.2)
forcats 0.4.0 2019-02-17 [1] CRAN (R 3.6.2)
fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.2)
generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.2)
ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.2)
glue 1.4.0 2020-04-03 [1] CRAN (R 3.6.3)
gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.2)
hms 0.5.3 2020-01-08 [1] CRAN (R 3.6.2)
htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.2)
htmlwidgets 1.5.1 2019-10-08 [1] CRAN (R 3.6.2)
httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.2)
kableExtra * 1.1.0 2019-03-16 [1] CRAN (R 3.6.3)
knitr * 1.28 2020-02-06 [1] CRAN (R 3.6.2)
labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0)
lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.2)
lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.3)
lubridate 1.7.8 2020-04-06 [1] CRAN (R 3.6.3)
magrittr * 1.5 2014-11-22 [1] CRAN (R 3.6.2)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.2)
munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.2)
pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.2)
pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.2)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.2)
pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.2)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.2)
processx 3.4.2 2020-02-09 [1] CRAN (R 3.6.2)
ps 1.3.2 2020-02-13 [1] CRAN (R 3.6.2)
purrr 0.3.4 2020-04-17 [1] CRAN (R 3.6.3)
R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.2)
RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 3.6.0)
Rcpp 1.0.4.6 2020-04-09 [1] CRAN (R 3.6.3)
readr 1.3.1 2018-12-21 [1] CRAN (R 3.6.2)
remotes 2.1.1 2020-02-15 [1] CRAN (R 3.6.2)
rlang 0.4.5 2020-03-01 [1] CRAN (R 3.6.3)
rmarkdown * 2.1 2020-01-20 [1] CRAN (R 3.6.2)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.2)
rstudioapi 0.11 2020-02-07 [1] CRAN (R 3.6.2)
rvest 0.3.5 2019-11-08 [1] CRAN (R 3.6.2)
scales 1.1.0 2019-11-18 [1] CRAN (R 3.6.2)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.2)
stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.2)
stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.2)
testthat 2.3.2 2020-03-02 [1] CRAN (R 3.6.3)
tibble 3.0.1 2020-04-20 [1] CRAN (R 3.6.3)
tidyselect 1.0.0 2020-01-27 [1] CRAN (R 3.6.2)
usethis 1.6.0 2020-04-09 [1] CRAN (R 3.6.3)
vctrs 0.2.4 2020-03-10 [1] CRAN (R 3.6.3)
viridisLite 0.3.0 2018-02-01 [1] CRAN (R 3.6.2)
webshot 0.5.2 2019-11-22 [1] CRAN (R 3.6.3)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.2)
xfun 0.12 2020-01-13 [1] CRAN (R 3.6.2)
xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.2)
yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.2)
[1] C:/Users/an499583/Documents/R/win-library/3.6
[2] C:/Users/an499583/Documents/R/R-3.6.3/library